﻿


Element = function() {
    var Id;
    var Text;
    var TranslatedText;
    var Name;
};

var _Elements = new Array();
var _Id = 0;

function trim(str) {
    if (!str || typeof str != 'string')
        return null;

    str = str.replace(/^[\s]+/, '').replace(/[\s]+$/, '').replace(/[\s]{2,}/, ' ').replace(/^[\n]+/, '');
    str = str.replace(/^[\<]+/, '&lt;').replace(/^[\>]+/, '&gt;').replace('»', '').replace('©', '').replace(' ', ' ');
    str = str.replace(/\"/g, '');
    str = str.replace(/\'/g, "");
    return str;
}

function ParseDocument(o) {
    var txt = '';
    for (var i = 0; i < o.childNodes.length; i++) {
        switch (o.childNodes[i].nodeType) {
            case 1:
                if (o.childNodes[i].nodeName != "SCRIPT" && o.childNodes[i].nodeName != "STYLE" && o.childNodes[i].nodeName != "NOSCRIPT") {
                    ParseDocument(o.childNodes[i]);
                }
                break
            case 3: txt = o.childNodes[i].nodeValue;

                txt = trim(txt);
                if (txt != "") {
                    var _Element = new Element();
                    if (o.id == null || o.id == "undefined" || o.id == "") {
                        o.setAttribute('id', "NfId_" + _Id)
                        //o.id = "NfId_" + _Id;
                        _Id = _Id + 1;
                    }

                    _Element.Id = o.id;
                    _Element.Text = txt;
                    _Element.Name = o.nodeName;
                    _Elements[_Elements.length] = _Element;
                }
                break
            case 8: "";
                break
        }
    }
    //return txt;
}

function SetText() {
    for (i = 0; i < _Elements.length; i++) {
        SetText(_Elements[i].Id,_Elements[i].TranslatedText);
    }
}

function SetText(Id,Text) {
    if (document.getElementById(Id) != null) {
        switch (_Elements[i].Name) {
            case "INPUT":
                document.getElementById(Id).text = Text;
                break;
            default:
                document.getElementById(Id).innerHTML = Text;
                break;
        }
    }
}


function PreProcess() {
    var _RawText = "";
    for (i = 0; i < _Elements.length; i++) {
        _RawText += _Elements[i].Id + '\t' + _Elements[i].Text + '\n';
    }
    return _RawText;
}
/*
function PostService(xml) {
    $(xml).find("Text").each(function() {
        var _Id = $(this).attr("id");
        var _Text = $(this).attr("Text");
        ChangeObjectValue(_Id, _Text);
    });
    SetText();
}
*/
function PostService(Result) {
    var _RArray = Result.split('\n');
    for (i = 0; i < _RArray.length; i++) {
        var Temp = _RArray[i].split('\t');
        //ChangeObjectValue(Temp[0], Temp[1]);
        SetText(Temp[0], Temp[1]);
    }
    //SetText();
}

function ChangeObjectValue(Id, Text) {
    for (i = 0; i < _Elements.length; i++) {
        if (_Elements[i].Id == Id) {
            _Elements[i].TranslatedText = Text;
            break;
        }
    }
}

function CallService() {
    var webMethod = 'http://localhost:17279/TestService.asmx/TranslateWebSite'
    var parameters = "{'Text':'" + PreProcess() + "'}"

    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            PostService(msg.d);
        },
        error: function(e) {
            alert(e.responseText);
        }
    });
}